﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Web;
using System.Web.Mvc;
using S4Store.Models.Report;
using S4Store.Domain;
using S4Store.Service;
using S4Store.Domain.AfterSaleKpi;
using System.Web.UI.DataVisualization.Charting;
using System.Drawing;
using S4Store.Models;
using S4Store.Models.AfterSaleReport;

namespace S4Store.Controllers
{
    public class SparePartReportController : ReportBaseController
    {
        //
        // GET: /SparePartReport/
        public SparePartService SparePartService { get; set; }
        public DeadStockService DeadStockService { get; set; }
        public AfterSaleCarKPIService AfterSaleCarKPIService { get; set; }

        public ActionResult Index()
        {
            return View();
        }

        public ActionResult SparePartIndex()
        {
            SparePartModel model = new SparePartModel();
            using (var t = new Transtion())
            {
                decimal 时间进度 = (decimal)Month.LastOrNowDay() / (decimal)(Month.LastDay());
                t.Inject(SparePartService);

                model.MonthConfigs = SparePartService.GetQuery().Where(o => o.Month.Year==Month.Year && o.Month.Month==Month.Month && o.StoreId==Store.Id && o.IsConfirmed).ToList();


                ViewBag.时间进度 = 时间进度;

                this.InjectReportModel(model);
                model.BarModel.Action = "SparePartIndex";
                model.BarModel.Controller = "SparePartReport";
                //model.BarModel.PlusItem = "<a target='navtab' class='historyrecord' title='各车型用品销售状况KPI历史数据查询' href='" + Url.Action("CarDecoratingSaleHistory") + "' ><span>历史数据查询</span></a>";
                model.BarModel.AddView(ReportBarModel.ViewItem.Month);
                t.Commit();
            }
            return View(model);
        }

        //在库天数
        public ActionResult ZKTS()
        {
            SparePartModel model = new SparePartModel();
            DateTime start = DateTime.Now.AddMonths(-6);
            DateTime end = DateTime.Now;
            if (this.Start != null)
            {
                start = new DateTime(Start.Value.Year, Start.Value.Month, 1);
                end = new DateTime(End.Value.Year, End.Value.Month, 1);
            }
            using (var t = new Transtion())
            {
                decimal 时间进度 = (decimal)Month.LastOrNowDay() / (decimal)(Month.LastDay());
                t.Inject(SparePartService);
                t.Inject(AfterSaleCarKPIService);

                model.MonthConfigs = SparePartService.GetQuery().Where(o => o.Month <= end && o.Month >= start && o.StoreId == Store.Id && o.IsConfirmed).ToList();
                model.StandardConfigs = AfterSaleCarKPIService.GetAfterSaleMonthStandardConfigs().Where(o => o.Month <= end && o.Month >= start && o.StoreId == Store.Id).ToList();

                model.MonthConfig = SparePartService.GetQuery().Where(o => o.Month.Year == Month.Year && o.Month.Month == Month.Month && o.IsConfirmed && o.StoreId == Store.Id).ToList();
                model.StandardConfig = AfterSaleCarKPIService.GetAfterSaleMonthStandardConfigs().Where(o => o.Month.Year == Month.Year && o.Month.Month == Month.Month && o.StoreId == Store.Id).ToList();


                ViewBag.时间进度 = 时间进度;

                this.InjectReportModel(model);
                model.BarModel.Action = "ZKTS";
                model.BarModel.Controller = "SparePartReport";
                model.BarModel.Start = start;
                model.BarModel.End = end;
				if (
                           CurrentUser.Position == S4Store.Domain.Position.SystemManager ||
                           CurrentUser.Position == S4Store.Domain.Position.DealerTutors
                           )
		{
                model.BarModel.PlusItem = "<a class='historyrecord edit'  backtype='html' href='" + Url.Action("AddImproveGuide", "ImproveGuide", new { guidetype = S4Store.Domain.ImproveGuide.ImproveGuideType.AfterSale.ToInt() ,referdesc="售后管理>备件库存动态>进销管理>平均在库天数" }) + "' target='dialog' width='60%'><span >填写改善指导建议</span></a>";
		}
                model.BarModel.AddView(ReportBarModel.ViewItem.StartEnd);
            }
            return View(model);
        }

        public ActionResult BJZZL()
        {
            SparePartModel model = new SparePartModel();
            DateTime start = DateTime.Now.AddMonths(-6);
            DateTime end = DateTime.Now;
            if (this.Start != null)
            {
                start = new DateTime(Start.Value.Year, Start.Value.Month, 1);
                end = new DateTime(End.Value.Year, End.Value.Month, 1);
            }
            using (var t = new Transtion())
            {
                decimal 时间进度 = (decimal)Month.LastOrNowDay() / (decimal)(Month.LastDay());
                t.Inject(SparePartService);
                t.Inject(AfterSaleCarKPIService);

                model.MonthConfigs = SparePartService.GetQuery().Where(o => o.Month <= end && o.Month >= start && o.StoreId == Store.Id && o.IsConfirmed).ToList();
                model.StandardConfigs = AfterSaleCarKPIService.GetAfterSaleMonthStandardConfigs().Where(o => o.Month <= end && o.Month >= start && o.StoreId == Store.Id).ToList();

                model.MonthConfig = SparePartService.GetQuery().Where(o => o.Month.Year == Month.Year && o.Month.Month == Month.Month && o.IsConfirmed && o.StoreId == Store.Id).ToList();
                model.StandardConfig = AfterSaleCarKPIService.GetAfterSaleMonthStandardConfigs().Where(o => o.Month.Year == Month.Year && o.Month.Month == Month.Month && o.StoreId == Store.Id).ToList();


                ViewBag.时间进度 = 时间进度;

                this.InjectReportModel(model);
                model.BarModel.Action = "BJZZL";
                model.BarModel.Controller = "SparePartReport";
                model.BarModel.Start = start;
                model.BarModel.End = end;
				if (
                           CurrentUser.Position == S4Store.Domain.Position.SystemManager ||
                           CurrentUser.Position == S4Store.Domain.Position.DealerTutors
                           )
		{
                model.BarModel.PlusItem = "<a class='historyrecord edit'  backtype='html' href='" + Url.Action("AddImproveGuide", "ImproveGuide", new { guidetype = S4Store.Domain.ImproveGuide.ImproveGuideType.AfterSale.ToInt() ,referdesc="售后管理>备件库存动态>进销管理>备件周转率" }) + "' target='dialog' width='60%'><span >填写改善指导建议</span></a>";
		}
                model.BarModel.AddView(ReportBarModel.ViewItem.StartEnd);
            }
            return View(model);
        }

        public ActionResult BJBXHGL()
        {
            SparePartModel model = new SparePartModel();
            DateTime start = DateTime.Now.AddMonths(-6);
            DateTime end = DateTime.Now;
            if (this.Start != null)
            {
                start = new DateTime(Start.Value.Year, Start.Value.Month, 1);
                end = new DateTime(End.Value.Year, End.Value.Month, 1);
            }
            using (var t = new Transtion())
            {
                decimal 时间进度 = (decimal)Month.LastOrNowDay() / (decimal)(Month.LastDay());
                t.Inject(SparePartService);
                t.Inject(AfterSaleCarKPIService);

                model.MonthConfigs = SparePartService.GetQuery().Where(o => o.Month <= end && o.Month >= start && o.StoreId == Store.Id && o.IsConfirmed).ToList();
                model.StandardConfigs = AfterSaleCarKPIService.GetAfterSaleMonthStandardConfigs().Where(o => o.Month <= end && o.Month >= start && o.StoreId == Store.Id).ToList();

                model.MonthConfig = SparePartService.GetQuery().Where(o => o.Month.Year == Month.Year && o.Month.Month == Month.Month && o.IsConfirmed && o.StoreId == Store.Id).ToList();
                model.StandardConfig = AfterSaleCarKPIService.GetAfterSaleMonthStandardConfigs().Where(o => o.Month.Year == Month.Year && o.Month.Month == Month.Month && o.StoreId == Store.Id).ToList();


                ViewBag.时间进度 = 时间进度;

                this.InjectReportModel(model);
                model.BarModel.Action = "BJBXHGL";
                model.BarModel.Controller = "SparePartReport";
                model.BarModel.Start = start;
                model.BarModel.End = end;
				if (
                           CurrentUser.Position == S4Store.Domain.Position.SystemManager ||
                           CurrentUser.Position == S4Store.Domain.Position.DealerTutors
                           )
		{
                model.BarModel.PlusItem = "<a class='historyrecord edit'  backtype='html' href='" + Url.Action("AddImproveGuide", "ImproveGuide", new { guidetype = S4Store.Domain.ImproveGuide.ImproveGuideType.AfterSale.ToInt() ,referdesc="售后管理>备件库存动态>进销管理>备件保修合格率" }) + "' target='dialog' width='60%'><span >填写改善指导建议</span></a>";
		}
                model.BarModel.AddView(ReportBarModel.ViewItem.StartEnd);
            }
            return View(model);
        }
        public ActionResult ZCDHL()
        {
            SparePartModel model = new SparePartModel();
            DateTime start = DateTime.Now.AddMonths(-6);
            DateTime end = DateTime.Now;
            if (this.Start != null)
            {
                start = new DateTime(Start.Value.Year, Start.Value.Month, 1);
                end = new DateTime(End.Value.Year, End.Value.Month, 1);
            }
            using (var t = new Transtion())
            {
                decimal 时间进度 = (decimal)Month.LastOrNowDay() / (decimal)(Month.LastDay());
                t.Inject(SparePartService);
                t.Inject(AfterSaleCarKPIService);

                model.MonthConfigs = SparePartService.GetQuery().Where(o => o.Month <= end && o.Month >= start && o.StoreId == Store.Id && o.IsConfirmed).ToList();
                model.StandardConfigs = AfterSaleCarKPIService.GetAfterSaleMonthStandardConfigs().Where(o => o.Month <= end && o.Month >= start && o.StoreId == Store.Id).ToList();

                model.MonthConfig = SparePartService.GetQuery().Where(o => o.Month.Year == Month.Year && o.Month.Month == Month.Month && o.IsConfirmed && o.StoreId == Store.Id).ToList();
                model.StandardConfig = AfterSaleCarKPIService.GetAfterSaleMonthStandardConfigs().Where(o => o.Month.Year == Month.Year && o.Month.Month == Month.Month && o.StoreId == Store.Id).ToList();


                ViewBag.时间进度 = 时间进度;

                this.InjectReportModel(model);
                model.BarModel.Action = "ZCDHL";
                model.BarModel.Controller = "SparePartReport";
                model.BarModel.Start = start;
                model.BarModel.End = end;
				if (
                           CurrentUser.Position == S4Store.Domain.Position.SystemManager ||
                           CurrentUser.Position == S4Store.Domain.Position.DealerTutors
                           )
		{
                model.BarModel.PlusItem = "<a class='historyrecord edit'  backtype='html' href='" + Url.Action("AddImproveGuide", "ImproveGuide", new { guidetype = S4Store.Domain.ImproveGuide.ImproveGuideType.AfterSale.ToInt() ,referdesc="售后管理>备件库存动态>进销管理>正常订货率" }) + "' target='dialog' width='60%'><span >填写改善指导建议</span></a>";
		}
                model.BarModel.AddView(ReportBarModel.ViewItem.StartEnd);
            }
            return View(model);
        }


        public ActionResult BJMZL()
        {
            SparePartModel model = new SparePartModel();
            DateTime start = DateTime.Now.AddMonths(-6);
            DateTime end = DateTime.Now;
            if (this.Start != null)
            {
                start = new DateTime(Start.Value.Year, Start.Value.Month, 1);
                end = new DateTime(End.Value.Year, End.Value.Month, 1);
            }
            using (var t = new Transtion())
            {
                decimal 时间进度 = (decimal)Month.LastOrNowDay() / (decimal)(Month.LastDay());
                t.Inject(SparePartService);
                t.Inject(AfterSaleCarKPIService);

                model.MonthConfigs = SparePartService.GetQuery().Where(o => o.Month <= end && o.Month >= start && o.StoreId == Store.Id && o.IsConfirmed).ToList();
                model.StandardConfigs = AfterSaleCarKPIService.GetAfterSaleMonthStandardConfigs().Where(o => o.Month <= end && o.Month >= start && o.StoreId == Store.Id).ToList();

                model.MonthConfig = SparePartService.GetQuery().Where(o => o.Month.Year == Month.Year && o.Month.Month == Month.Month && o.IsConfirmed && o.StoreId == Store.Id).ToList();
                model.StandardConfig = AfterSaleCarKPIService.GetAfterSaleMonthStandardConfigs().Where(o => o.Month.Year == Month.Year && o.Month.Month == Month.Month && o.StoreId == Store.Id).ToList();


                ViewBag.时间进度 = 时间进度;

                this.InjectReportModel(model);
                model.BarModel.Action = "BJMZL";
                model.BarModel.Controller = "SparePartReport";
                model.BarModel.Start = start;
                model.BarModel.End = end;
				if (
                           CurrentUser.Position == S4Store.Domain.Position.SystemManager ||
                           CurrentUser.Position == S4Store.Domain.Position.DealerTutors
                           )
		{
                model.BarModel.PlusItem = "<a class='historyrecord edit'  backtype='html' href='" + Url.Action("AddImproveGuide", "ImproveGuide", new { guidetype = S4Store.Domain.ImproveGuide.ImproveGuideType.AfterSale.ToInt() ,referdesc="售后管理>备件库存动态>进销管理>备件满足率" }) + "' target='dialog' width='60%'><span >填写改善指导建议</span></a>";
		}
                model.BarModel.AddView(ReportBarModel.ViewItem.StartEnd);
            }
            return View(model);
        }

        public ActionResult RemainingInventory()
        {
            SparePartModel model = new SparePartModel();
            using (var t = new Transtion())
            {
                decimal 时间进度 = (decimal)Month.LastOrNowDay() / (decimal)(Month.LastDay());
                t.Inject(SparePartService);
                t.Inject(AfterSaleCarKPIService);

                model.MonthConfigs = SparePartService.GetQuery().Where(o => o.Month.Year == Month.Year && o.Month.Month == Month.Month && o.StoreId == Store.Id && o.IsConfirmed).ToList();
                model.StandardConfigs = AfterSaleCarKPIService.GetAfterSaleMonthStandardConfigs(Month.Year, Month.Month, Store.Id);

                ViewBag.时间进度 = 时间进度;

                this.InjectReportModel(model);
                model.BarModel.Action = "RemainingInventory";
                model.BarModel.Controller = "SparePartReport";
                //model.BarModel.PlusItem = "<a target='navtab' class='historyrecord' title='各车型用品销售状况KPI历史数据查询' href='" + Url.Action("CarDecoratingSaleHistory") + "' ><span>历史数据查询</span></a>";
                model.BarModel.AddView(ReportBarModel.ViewItem.Month);
                t.Commit();
            }
            return View(model);
        }

        public ActionResult DZKBHL()
        {
            SparePartModel model = new SparePartModel();
            DateTime start = DateTime.Now.AddMonths(-6);
            DateTime end = DateTime.Now;
            if (this.Start != null)
            {
                start = new DateTime(Start.Value.Year, Start.Value.Month, 1);
                end = new DateTime(End.Value.Year, End.Value.Month, 1);
            }
            using (var t = new Transtion())
            {
                decimal 时间进度 = (decimal)Month.LastOrNowDay() / (decimal)(Month.LastDay());
                t.Inject(SparePartService);
                t.Inject(AfterSaleCarKPIService);

                model.MonthConfigs = SparePartService.GetQuery().Where(o => o.Month <= end && o.Month >= start && o.StoreId == Store.Id && o.IsConfirmed).ToList();
                model.StandardConfigs = AfterSaleCarKPIService.GetAfterSaleMonthStandardConfigs().Where(o => o.Month <= end && o.Month >= start && o.StoreId == Store.Id).ToList();

                model.MonthConfig = SparePartService.GetQuery().Where(o => o.Month.Year == Month.Year && o.Month.Month == Month.Month && o.IsConfirmed && o.StoreId == Store.Id).ToList();
                model.StandardConfig = AfterSaleCarKPIService.GetAfterSaleMonthStandardConfigs().Where(o => o.Month.Year == Month.Year && o.Month.Month == Month.Month && o.StoreId == Store.Id).ToList();


                ViewBag.时间进度 = 时间进度;

                this.InjectReportModel(model);
                model.BarModel.Action = "DZKBHL";
                model.BarModel.Controller = "SparePartReport";
                model.BarModel.Start = start;
                model.BarModel.End = end;
				if (
                           CurrentUser.Position == S4Store.Domain.Position.SystemManager ||
                           CurrentUser.Position == S4Store.Domain.Position.DealerTutors
                           )
		{
                model.BarModel.PlusItem = "<a class='historyrecord edit'  backtype='html' href='" + Url.Action("AddImproveGuide", "ImproveGuide", new { guidetype = S4Store.Domain.ImproveGuide.ImproveGuideType.AfterSale.ToInt() ,referdesc="售后管理>备件库存动态>滞留库存>呆库存变化率" }) + "' target='dialog' width='60%'><span >填写改善指导建议</span></a> <a class='historyrecord edit'  backtype='html' href='" + Url.Action("DZKDetail", new { month = Month }) + "' target=\"navtab\"><span >呆滞库明细查询</span></a> ";
				}else
		{
			 model.BarModel.PlusItem = "<a class='historyrecord edit'  backtype='html' href='" + Url.Action("DZKDetail", new { month = Month }) + "' target=\"navtab\"><span >呆滞库明细查询</span></a> ";
			}
                model.BarModel.AddView(ReportBarModel.ViewItem.StartEnd);
            }
            return View(model);
        }

        public ActionResult DZKDetail()
        {
            SparePartModel model = new SparePartModel();
            using (var t = new Transtion())
            {
                t.Inject(DeadStockService);
                t.Inject(SparePartService);

                model.MonthConfigs = SparePartService.GetQuery().Where(o => o.Month.Year == Month.Year && o.Month.Month == Month.Month && o.IsConfirmed && o.StoreId == Store.Id).ToList();
                model.DeadStockMonthConfigs = DeadStockService.GetQuery().Where(o => o.Month.Month == Month.Month && o.Month.Year == Month.Year && o.StoreId == Store.Id && o.IsConfirmed && o.Cla==0).ToList();

                this.InjectReportModel(model);
                model.BarModel.Action = "DZKDetail";
                model.BarModel.Controller = "SparePartReport";
                model.BarModel.AddView(ReportBarModel.ViewItem.Month);
            }
            return View(model);
        }


        public ActionResult SKCBHL()
        {
            SparePartModel model = new SparePartModel();
            DateTime start = DateTime.Now.AddMonths(-6);
            DateTime end = DateTime.Now;
            if (this.Start != null)
            {
                start = new DateTime(Start.Value.Year, Start.Value.Month, 1);
                end = new DateTime(End.Value.Year, End.Value.Month, 1);
            }
            using (var t = new Transtion())
            {
                decimal 时间进度 = (decimal)Month.LastOrNowDay() / (decimal)(Month.LastDay());
                t.Inject(SparePartService);
                t.Inject(AfterSaleCarKPIService);

                model.MonthConfigs = SparePartService.GetQuery().Where(o => o.Month <= end && o.Month >= start && o.StoreId == Store.Id && o.IsConfirmed).ToList();
                model.StandardConfigs = AfterSaleCarKPIService.GetAfterSaleMonthStandardConfigs().Where(o => o.Month <= end && o.Month >= start && o.StoreId == Store.Id).ToList();

                model.MonthConfig = SparePartService.GetQuery().Where(o => o.Month.Year == Month.Year && o.Month.Month == Month.Month && o.IsConfirmed && o.StoreId == Store.Id).ToList();
                model.StandardConfig = AfterSaleCarKPIService.GetAfterSaleMonthStandardConfigs().Where(o => o.Month.Year == Month.Year && o.Month.Month == Month.Month && o.StoreId == Store.Id).ToList();


                ViewBag.时间进度 = 时间进度;

                this.InjectReportModel(model);
                model.BarModel.Action = "SKCBHL";
                model.BarModel.Controller = "SparePartReport";
                model.BarModel.Start = start;
                model.BarModel.End = end;
				if (
                           CurrentUser.Position == S4Store.Domain.Position.SystemManager ||
                           CurrentUser.Position == S4Store.Domain.Position.DealerTutors
                           )
		{
                model.BarModel.PlusItem = "<a class='historyrecord edit'  backtype='html' href='" + Url.Action("AddImproveGuide", "ImproveGuide", new { guidetype = S4Store.Domain.ImproveGuide.ImproveGuideType.AfterSale.ToInt() ,referdesc="售后管理>备件库存动态>滞留库存>死库存变化率" }) + "' target='dialog' width='60%'><span >填写改善指导建议</span></a> <a class='historyrecord edit'  backtype='html' href='" + Url.Action("SKCDetail", new { month = Month }) + "' target=\"navtab\"><span >死库明细查询</span></a> ";
				}else
		{
			  model.BarModel.PlusItem = "<a class='historyrecord edit'  backtype='html' href='" + Url.Action("SKCDetail", new { month = Month }) + "' target=\"navtab\"><span >死库明细查询</span></a> ";
			}
                model.BarModel.AddView(ReportBarModel.ViewItem.StartEnd);
            }
            return View(model);
        }

        public ActionResult SKCDetail()
        {
            SparePartModel model = new SparePartModel();
            using (var t = new Transtion())
            {
                t.Inject(DeadStockService);
                t.Inject(SparePartService);

                model.MonthConfigs = SparePartService.GetQuery().Where(o => o.Month.Year == Month.Year && o.Month.Month == Month.Month && o.IsConfirmed && o.StoreId == Store.Id).ToList();
                model.DeadStockMonthConfigs = DeadStockService.GetQuery().Where(o => o.Month.Month == Month.Month && o.Month.Year == Month.Year && o.StoreId == Store.Id && o.IsConfirmed && o.Cla == 1).ToList();

                this.InjectReportModel(model);
                model.BarModel.Action = "SKCDetail";
                model.BarModel.Controller = "SparePartReport";
                model.BarModel.AddView(ReportBarModel.ViewItem.Month);
            }
            return View(model);
        }

    }
}
